package com.vincent.filepickersample;

import com.vincent.filepicker.Constant;
import com.vincent.filepicker.ability.AudioPickAbility;
import com.vincent.filepicker.ability.ImagePickAbility;
import com.vincent.filepicker.ability.NormalFilePickAbility;
import com.vincent.filepicker.ability.VideoPickAbility;
import com.vincent.filepicker.filter.entity.AudioFile;
import com.vincent.filepicker.filter.entity.ImageFile;
import com.vincent.filepicker.filter.entity.NormalFile;
import com.vincent.filepicker.filter.entity.VideoFile;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.util.ArrayList;

import static com.vincent.filepicker.ability.AudioPickAbility.IS_NEED_RECORDER;
import static com.vincent.filepicker.ability.BaseAbility.IS_NEED_FOLDER_LIST;
import static com.vincent.filepicker.ability.ImagePickAbility.IS_NEED_CAMERA;

public class MainAbility extends Ability implements Component.ClickedListener {

    private Text mTvResult;
    public static final int RESULT_OK = -1;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);

        mTvResult = (Text) findComponentById(ResourceTable.Id_tv_result);
        findComponentById(ResourceTable.Id_btn_pick_image).setClickedListener(this::onClick);
        findComponentById(ResourceTable.Id_btn_pick_video).setClickedListener(this::onClick);
        findComponentById(ResourceTable.Id_btn_pick_audio).setClickedListener(this::onClick);
        findComponentById(ResourceTable.Id_btn_pick_file).setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component v) {
        int id = v.getId();
        switch (id) {
            case ResourceTable.Id_btn_pick_image:
                Intent intent1 = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(ImagePickAbility.class.getName())
                        .build();
                intent1.setOperation(operation);
                intent1.setParam(IS_NEED_CAMERA, true);
                intent1.setParam(Constant.MAX_NUMBER, 9);
                intent1.setParam(IS_NEED_FOLDER_LIST, true);
                startAbilityForResult(intent1, Constant.REQUEST_CODE_PICK_IMAGE);
                break;
            case ResourceTable.Id_btn_pick_video:
                Intent intent2 = new Intent();
                Operation operation2 = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(VideoPickAbility.class.getName())
                        .build();
                intent2.setOperation(operation2);
                intent2.setParam(IS_NEED_CAMERA, true);
                intent2.setParam(Constant.MAX_NUMBER, 9);
                intent2.setParam(IS_NEED_FOLDER_LIST, true);
                startAbilityForResult(intent2, Constant.REQUEST_CODE_PICK_VIDEO);
                break;
            case ResourceTable.Id_btn_pick_audio:
                Intent intent3 = new Intent();
                Operation operation3 = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(AudioPickAbility.class.getName())
                        .build();
                intent3.setOperation(operation3);
                intent3.setParam(IS_NEED_RECORDER, true);
                intent3.setParam(Constant.MAX_NUMBER, 9);
                intent3.setParam(IS_NEED_FOLDER_LIST, true);
                startAbilityForResult(intent3, Constant.REQUEST_CODE_PICK_AUDIO);
                break;
            case ResourceTable.Id_btn_pick_file:
                Intent intent4 = new Intent();
                Operation operation4 = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(NormalFilePickAbility.class.getName())
                        .build();
                intent4.setOperation(operation4);
                intent4.setParam(Constant.MAX_NUMBER, 9);
                intent4.setParam(IS_NEED_FOLDER_LIST, true);
                intent4.setParam(NormalFilePickAbility.SUFFIX,
                        new String[]{"xlsx", "xls", "doc", "dOcX", "ppt", ".pptx", "pdf"});
                startAbilityForResult(intent4, Constant.REQUEST_CODE_PICK_FILE);
                break;
        }
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent data) {
        super.onAbilityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Constant.REQUEST_CODE_PICK_IMAGE:
                if (resultCode == RESULT_OK) {
                    ArrayList<ImageFile> list = data.getSequenceableArrayListParam(Constant.RESULT_PICK_IMAGE);
                    StringBuilder builder = new StringBuilder();
                    for (ImageFile file : list) {
                        String path = file.getPath();
                        builder.append(path + "\n");
                    }
                    mTvResult.setText(builder.toString());
                }
                break;
            case Constant.REQUEST_CODE_PICK_VIDEO:
                if (resultCode == RESULT_OK) {
                    ArrayList<VideoFile> list = data.getSequenceableArrayListParam(Constant.RESULT_PICK_VIDEO);
                    StringBuilder builder = new StringBuilder();
                    for (VideoFile file : list) {
                        String path = file.getPath();
                        builder.append(path + "\n");
                    }
                    mTvResult.setText(builder.toString());
                }
                break;
            case Constant.REQUEST_CODE_PICK_AUDIO:
                if (resultCode == RESULT_OK) {
                    ArrayList<AudioFile> list = data.getSequenceableArrayListParam(Constant.RESULT_PICK_AUDIO);
                    StringBuilder builder = new StringBuilder();
                    for (AudioFile file : list) {
                        String path = file.getPath();
                        builder.append(path + "\n");
                    }
                    mTvResult.setText(builder.toString());
                }
                break;
            case Constant.REQUEST_CODE_PICK_FILE:
                if (resultCode == RESULT_OK) {
                    ArrayList<NormalFile> list = data.getSequenceableArrayListParam(Constant.RESULT_PICK_FILE);
                    StringBuilder builder = new StringBuilder();
                    for (NormalFile file : list) {
                        String path = file.getPath();
                        builder.append(path + "\n");
                    }
                    mTvResult.setText(builder.toString());
                }
                break;
        }
    }

}
